//
//  NBLoginView.swift
//  NBSinaWeibo
//
//  Created by ac on 16/5/20.
//  Copyright © 2016年 ac. All rights reserved.
//

import UIKit
import SnapKit

//@objc protocol NBLoginViewDelegate:NSObjectProtocol {
//    optional func userWillLogin()
//    func userWillLogup()
//}

class NBLoginView: UIView {
    
    var userLogin :(()->())?
    var userLogup :(()->())?
    
//    weak var loginViewDelegate: NBLoginViewDelegate?
    private lazy var centerIcon:UIImageView = {
        let image = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return image
    }()
    private lazy var bigHouse:UIImageView = {
        let big = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return big
    }()
    
    private lazy var coverIcon: UIImageView = {
        let c = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return c
    }()
    private lazy var textLbl: UILabel = {
        let t = UILabel()
        t.text = "关注一些人,回到这里看看有什么惊喜"
        t.numberOfLines = 0
        t.textAlignment = .Center
        t.font = UIFont.systemFontOfSize(14)
        t.textColor = UIColor.darkGrayColor()
        t.preferredMaxLayoutWidth = 230
        t.sizeToFit()
        return t
    }()
    private lazy var loginBtn: UIButton = {
        let loginB = UIButton()
        loginB.setTitle("登录", forState: .Normal)
        loginB.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: .Normal)
        loginB.titleLabel?.font = UIFont.systemFontOfSize(15)
        loginB.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        loginB.addTarget(self, action: #selector(loginBtnClick), forControlEvents: .TouchUpInside)
        return loginB
    }()
    private lazy var logupBtn: UIButton = {
        let logupB = UIButton()
        logupB.setTitle("注册", forState: .Normal)
        logupB.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: .Normal)
        logupB.titleLabel?.font = UIFont.systemFontOfSize(15)
        logupB.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        logupB.addTarget(self, action: #selector(logupBtnClick), forControlEvents: .TouchUpInside)
        return logupB
    }()
    
    @objc private func loginBtnClick(){
//        loginViewDelegate?.userWillLogin?()
        
        userLogin?()
    }
    
    @objc private func logupBtnClick(){
//        loginViewDelegate?.userWillLogup()
        
        userLogup?()
    }
    
    func updateUI(imageName: String,lblText: String,isHome: Bool = false) {
        if isHome {
            startAnimation()
        }else{
            bigHouse.hidden = true
            bringSubviewToFront(centerIcon)
        }
        centerIcon.image = UIImage(named: imageName)
        textLbl.text = lblText
    }
    
    private func startAnimation(){
        let baseAnimation = CABasicAnimation(keyPath: "transform.rotation")
        baseAnimation.duration = 15
        baseAnimation.repeatCount = MAXFLOAT
        baseAnimation.removedOnCompletion = false
        baseAnimation.toValue = 2 * M_PI
        centerIcon.layer.addAnimation(baseAnimation, forKey: nil)
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        let colorValue: CGFloat = 237.0 / 255
        
        self.backgroundColor = UIColor(red: colorValue, green: colorValue, blue: colorValue, alpha: 1)
        addView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func addView(){
        
        addSubview(centerIcon)
        addSubview(coverIcon)
        addSubview(bigHouse)
        addSubview(textLbl)
        addSubview(loginBtn)
        addSubview(logupBtn)
        
        
        centerIcon.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        coverIcon.snp_makeConstraints { (make) in
            make.center.equalTo(centerIcon.snp_center)
        }
        
        bigHouse.snp_makeConstraints { (make) in
            make.center.equalTo(centerIcon.snp_center)
        }
        
        textLbl.snp_makeConstraints { (make) in
            make.top.equalTo(centerIcon.snp_bottom).offset(16)
            make.centerX.equalTo(centerIcon.snp_centerX)
            make.height.equalTo(40)
        }
        
        loginBtn.snp_makeConstraints { (make) in
            make.left.equalTo(textLbl.snp_left)
            make.top.equalTo(textLbl.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        logupBtn.snp_makeConstraints { (make) in
            make.right.equalTo(textLbl.snp_right)
            make.top.equalTo(textLbl.snp_bottom).offset(16)
            make.width.equalTo(100)

        }
    
    }
}
